package com.tjl.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tjl.foodapp.utils.OkHttpClientUtil;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {

    Button registerBtn;
    EditText et_username,et_password,et_password2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        registerBtn = findViewById(R.id.registerBtn);
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        et_password2 = findViewById(R.id.et_password2);

        registerBtn.setOnClickListener(v -> {
            String username = et_username.getText().toString();
            String password = et_password.getText().toString();
            String password2 = et_password2.getText().toString();
            if (username.isEmpty() || password.isEmpty() || password2.isEmpty()) {
                Toast.makeText(this, "请输入账号或密码", Toast.LENGTH_SHORT).show();
            }
            if (!password.equals(password2)) {
                Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
            }
            new NetworkTask().execute();
        });
    }


    private class NetworkTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... voids) {
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("username", et_username.getText().toString());
                jsonObject.put("userpass", et_password.getText().toString());
                return OkHttpClientUtil.doPostJson(Utils.urlBase + "and_register.do", jsonObject.toString());
            } catch (Exception e) {
                e.printStackTrace();
                return e.getMessage();
            }
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (StringUtils.isNoneBlank(s)) {
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (jsonObject.getBoolean("flag")) {
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        //跳转到登录页面
                        finish();
                    } else {
                        Toast.makeText(RegisterActivity.this, jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(RegisterActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}